//
//  ZiDongBuHuoListModel.m
//  DA_V
//
//  Created by 中亿世联前台 on 15/5/16.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "ZiDongBuHuoListModel.h"
#import "AFNetworking.h"
#import "InterFace.h"
#import "EncryptionData.h"
#import "ProductModel.h"
#import "CategoryModel.h"
#import "JSONKit.h"
@implementation ZiDongBuHuoListModel

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}
#pragma -mark 向服务器获取客户已付款的订单
- (void)getZiDongList:(NSMutableArray *)array parameter:(NSDictionary *)parameter interface:(NSString *)interface{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:nil];
    
    
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    
    //    EncryptionData *enctytion = [[EncryptionData alloc] init];
    
    NSString *str = [self encodeString:jsonString key:PasswordKey];
    
    NSDictionary *paramer = @{@"sign":str};
    
    __weak ZiDongBuHuoListModel *ziDonglistModel = self;
    [manager POST:interface parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSString *str = responseObject[@"sign"];
        
        
        EncryptionData *data = [[EncryptionData alloc]init];
        NSString *datastr = [data decryption:str :PasswordKey];
        
        NSData* dataFromString = [datastr dataUsingEncoding:NSUTF8StringEncoding];
        
        NSDictionary *dataDic = [NSJSONSerialization  JSONObjectWithData:dataFromString options:NSJSONReadingMutableContainers error:nil];
        
        //返回成功
        if ( [[dataDic objectForKey:@"status"] isEqualToString:@"success"]) {
            
            [array removeAllObjects];
            
            NSLog(@"自动订单已付款返回结果 ＝＝＝＝ %@",dataDic);
            NSArray *arr = dataDic[@"data"];
            for (NSDictionary *myDic in arr) {
                ZiDongBuHuoListModel *model = [[ZiDongBuHuoListModel alloc] init];
                [model setValuesForKeysWithDictionary:myDic];
                [array addObject:model];
            }
            [ziDonglistModel.ziDongBuHuoListModelDelegate getZiDongBuHuoListEnd];
        } else if ( [[dataDic objectForKey:@"status"] isEqualToString:@"failed"]) {
            if ([[dataDic objectForKey:@"reason"] isEqualToString:@"没有找到相关订单......"]) {
               [ziDonglistModel.ziDongBuHuoListModelDelegate getZiDongBuHuoListEnd];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"暂无到相关订单" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                
            } else{
                 [ziDonglistModel.ziDongBuHuoListModelDelegate getZiDongBuHuoListEnd];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[dataDic objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        } else{
             [ziDonglistModel.ziDongBuHuoListModelDelegate getZiDongBuHuoListEnd];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"服务器异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [ziDonglistModel.ziDongBuHuoListModelDelegate getZiDongBuHuoListEnd];
        NSLog(@"========== %@", [error description]);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }];
    
}

#pragma -mark 向服务器上拉加载获取客户已付款的订单
- (void)getAddZiDongList:(NSMutableArray *)array parameter:(NSDictionary *)parameter interface:(NSString *)interface{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:nil];
    
    
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    
    //    EncryptionData *enctytion = [[EncryptionData alloc] init];
    
    NSString *str = [self encodeString:jsonString key:PasswordKey];
    
    NSDictionary *paramer = @{@"sign":str};
    
    __weak ZiDongBuHuoListModel *ziDonglistModel = self;
    [manager POST:interface parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSString *str = responseObject[@"sign"];
        
        
        EncryptionData *data = [[EncryptionData alloc]init];
        NSString *datastr = [data decryption:str :PasswordKey];
        
        NSData* dataFromString = [datastr dataUsingEncoding:NSUTF8StringEncoding];
        
        NSDictionary *dataDic = [NSJSONSerialization  JSONObjectWithData:dataFromString options:NSJSONReadingMutableContainers error:nil];
        
        //返回成功
        if ( [[dataDic objectForKey:@"status"] isEqualToString:@"success"]) {
            

            
            NSLog(@"自动订单已付款返回结果 ＝＝＝＝ %@",dataDic);
            NSArray *arr = dataDic[@"data"];
            for (NSDictionary *myDic in arr) {
                ZiDongBuHuoListModel *model = [[ZiDongBuHuoListModel alloc] init];
                [model setValuesForKeysWithDictionary:myDic];
                [array addObject:model];
            }
            [ziDonglistModel.ziDongBuHuoListModelDelegate getZiDongBuHuoListEnd];
        } else if ( [[dataDic objectForKey:@"status"] isEqualToString:@"failed"]) {
            if ([[dataDic objectForKey:@"reason"] isEqualToString:@"没有找到相关订单......"]) {
                [ziDonglistModel.ziDongBuHuoListModelDelegate getZiDongBuHuoListEnd];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"暂无到相关订单" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                
            } else{
                [ziDonglistModel.ziDongBuHuoListModelDelegate getZiDongBuHuoListEnd];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[dataDic objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        } else{
            [ziDonglistModel.ziDongBuHuoListModelDelegate getZiDongBuHuoListEnd];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"服务器异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [ziDonglistModel.ziDongBuHuoListModelDelegate getZiDongBuHuoListEnd];
        NSLog(@"========== %@", [error description]);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }];
    
}
#pragma -mark 向服务器获取自动补货订单详情
- (void)getZiDongList:(NSMutableArray *)array parameter:(NSDictionary *)parameter{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:nil];
    
    
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    
    //    EncryptionData *enctytion = [[EncryptionData alloc] init];
    
    NSString *str = [self encodeString:jsonString key:PasswordKey];
    
    NSDictionary *paramer = @{@"sign":str};
    
    __weak ZiDongBuHuoListModel *listModel = self;
    [manager POST:BuhuoInfoInterface parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        
        
        
        NSString *str = responseObject[@"sign"];
        
        
        EncryptionData *data = [[EncryptionData alloc]init];
        NSString *datastr = [data decryption:str :PasswordKey];
        
        NSData* dataFromString = [datastr dataUsingEncoding:NSUTF8StringEncoding];
        
        NSDictionary *dataDic = [NSJSONSerialization  JSONObjectWithData:dataFromString options:NSJSONReadingMutableContainers error:nil];
        
        //返回成功
        if ( [[dataDic objectForKey:@"status"] isEqualToString:@"success"]) {
            
            
            NSLog(@"获取自动补货订单详情返回结果 ＝＝＝＝ %@",dataDic);
            NSDictionary *mydataDic = dataDic[@"data"];
            
            [listModel setValuesForKeysWithDictionary:mydataDic];
            NSArray *dataArr = mydataDic[@"ProductInfo"];
            
            for (int i = 0; i< [dataArr count]; i++) {
      
                NSDictionary *aDic = dataArr[i];
                ZiDongBuHuoListModel *model = [[ZiDongBuHuoListModel alloc] init];
                [model setValuesForKeysWithDictionary:aDic];
                [array addObject:model];
            }
            
            [self.ziDongBuHuoListModelDelegate getZiDongBuHuoInfoListEnd];
        } else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"服务器异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"========== %@", [error description]);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }];
    
}


#pragma mark - 加密
-(NSString *)encodeString:(NSString *)data key:(NSString *)key{
    //    NSData* sampleData = [data dataUsingEncoding:NSUTF8StringEncoding];
    //
    //    NSString * base64String = [sampleData base64EncodedStringWithOptions:0];
    NSLog(@"%@",data);
    NSString *result=[[NSMutableString alloc] init];
    NSInteger keyLength = [key length];
    int position = 0;
    int keyChar = 0;
    int strChar = 0;
    for (int i = 0; i < [data length]; i++) {
        position = i % keyLength;
        strChar = [data characterAtIndex:i];
        keyChar =[key characterAtIndex:position];
        int  ch = strChar ^ keyChar;
        char ca = (char) ch;
        result=[result stringByAppendingString:[NSString stringWithFormat:@"%C",ca]];
    }
    
    NSData* sampleDat1a = [result dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString * base64String1 = [sampleDat1a base64EncodedStringWithOptions:0];
    
    return base64String1;
}

@end
